package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

/**
 * 订单对象视图
 * 
 * @version [版本号, 2012-10-24]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author hjd008
 */
public class OrderItemModel implements Serializable {

	private static final long serialVersionUID = -752260017219255298L;

	/**
	 * @Fields orderNumber : 订单号
	 */
	public String OrderNumber;

	/**
	 * 
	 * @Fields OrderDate : 时间
	 * 
	 */
	public String OrderDate;

	/**
	 * @Fields GrandTotal : 订单总价
	 */
	public double GrandTotal;
	public String CurrencyCode;
	
	public String CurrencyCodeStr;

	private boolean CanCancel;
	private boolean CanComplete;
	private boolean IsPaymentCompleted;
	private boolean CanRepay;
	private double DueAmount;
	private boolean IsCompleted;
	private boolean IsCancelled;
	private String OrderStatusStr;
	private boolean IsFullShipped;
	private boolean IsShipping;
	
	//物流信息新增字段
	private ArrayList<Package> packages;
	
	
	public boolean isIsFullShipped() {
		return IsFullShipped;
	}

	public ArrayList<Package> getPackages() {
		return packages;
	}

	public void setPackages(ArrayList<Package> packages) {
		this.packages = packages;
	}

	public void setIsFullShipped(boolean isFullShipped) {
		IsFullShipped = isFullShipped;
	}

	public boolean isIsShipping() {
		return IsShipping;
	}

	public void setIsShipping(boolean isShipping) {
		IsShipping = isShipping;
	}
	
	public boolean isIsCancelled() {
		return IsCancelled;
	}

	public void setIsCancelled(boolean isCancelled) {
		IsCancelled = isCancelled;
	}

	public String getOrderStatusStr() {
		return OrderStatusStr;
	}

	public void setOrderStatusStr(String orderStatusStr) {
		OrderStatusStr = orderStatusStr;
	}

	public boolean isIsCompleted() {
		return IsCompleted;
	}

	public void setIsCompleted(boolean isCompleted) {
		IsCompleted = isCompleted;
	}

	public boolean isCanCancel() {
		return CanCancel;
	}

	public void setCanCancel(boolean canCancel) {
		CanCancel = canCancel;
	}

	public boolean isCanComplete() {
		return CanComplete;
	}

	public void setCanComplete(boolean canComplete) {
		CanComplete = canComplete;
	}

	public boolean isIsPaymentCompleted() {
		return IsPaymentCompleted;
	}

	public void setIsPaymentCompleted(boolean isPaymentCompleted) {
		IsPaymentCompleted = isPaymentCompleted;
	}

	public boolean isCanRepay() {
		return CanRepay;
	}

	public void setCanRepay(boolean canRepay) {
		CanRepay = canRepay;
	}

	public double getDueAmount() {
		return DueAmount;
	}

	public void setDueAmount(double dueAmount) {
		DueAmount = dueAmount;
	}
	
	public String getCurrencyCodeStr() {
		return CurrencyCodeStr;
	}

	public void setCurrencyCodeStr(String currencyCodeStr) {
		CurrencyCodeStr = currencyCodeStr;
	}

	public String getCurrencyCode() {
		return CurrencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		CurrencyCode = currencyCode;
	}

	/**
	 * @Fields order status : 状态
	 */
	public String OrderStatus;

	/**
	 * @Fields Light : 页面展示的状态
	 */
	public String Light;

	// #########增加字段##############

	/**
	 * @Fields lines : 订单中的商品详细信息
	 */
	public List<Lines> lines;

	public List<Lines> getLines() {
		return lines;
	}

	public void setLines(List<Lines> lines) {
		this.lines = lines;
	}

	// ##############################

	public String getLight() {
		return Light;
	}

	public void setLight(String light) {
		Light = light;
	}

	public String getOrderNumber() {
		return OrderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		OrderNumber = orderNumber;
	}

	public String getOrderDate() {
		return OrderDate;
	}

	public void setOrderDate(String orderDate) {
		OrderDate = orderDate;
	}

	public double getGrandTotal() {
		return GrandTotal;
	}

	public void setGrandTotal(double grandTotal) {
		GrandTotal = grandTotal;
	}

	public String getOrderStatus() {
		return OrderStatus;
	}

	public void setOrderStatus(String orderStatus) {
		OrderStatus = orderStatus;
	}

	@Override
	public String toString() {
		return tojson().toString();
	}

	/**
	 * 实体转json对象
	 * 
	 * @return [参数说明]
	 * @return JSONObject [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public JSONObject tojson() {
		JSONObject json = new JSONObject();
		try {
			if (null != OrderNumber) {
				json.put(ServerConstant.Order.ORDERNUMBER, OrderNumber);
			}
			if (null != OrderDate) {
				json.put(ServerConstant.Order.ORDERDATE, OrderDate);
			}
			if (null != OrderStatus) {
				json.put(ServerConstant.Order.ORDERSTATUS, OrderStatus);
			}
			json.put(ServerConstant.Order.GRANDTOTAL, GrandTotal);

			if (null != Light) {
				json.put(ServerConstant.Order.LIGHT, Light);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * json转实体
	 * 
	 * @param json
	 * @return [参数说明]
	 * @return OrderItemModel [返回类型说明]
	 * @throws JSONException
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static OrderItemModel json2orderitem(JSONObject json)
			throws JSONException {
		if (null == json) {
			return null;
		}
		OrderItemModel orderitem = new OrderItemModel();
		if (json.optString(ServerConstant.Order.ORDERNUMBER) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERNUMBER)) {
			orderitem.setOrderNumber(json
					.optString(ServerConstant.Order.ORDERNUMBER));
		}
		if (json.optString(ServerConstant.Order.ORDERDATE) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERDATE)) {
			orderitem.setOrderDate(json
					.optString(ServerConstant.Order.ORDERDATE));
		}
		orderitem
				.setGrandTotal(json.optDouble(ServerConstant.Order.GRANDTOTAL));
		String currencyCode = json
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE);
		if (currencyCode != JSONObject.NULL
				&& !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE)) {
			orderitem.setCurrencyCode(currencyCode);
		}
		
		String currencyCodeStr = json
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE_STR);
		if (currencyCodeStr != JSONObject.NULL
				&& !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE_STR)) {
			orderitem.setCurrencyCodeStr(currencyCodeStr);
		}
		if (json.optString(json.optString(ServerConstant.Order.ORDERSTATUSSTR)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERSTATUSSTR)) {
			orderitem.setOrderStatusStr(json
					.optString(ServerConstant.Order.ORDERSTATUSSTR));
		}
		orderitem.setCanCancel(json.optBoolean(ServerConstant.Order.CAN_CANCEL));
		orderitem.setCanComplete(json.optBoolean(ServerConstant.Order.CAN_COMPLETE));
		orderitem.setIsPaymentCompleted(json.optBoolean(ServerConstant.Order.IS_PAYMENT_COMPLETED));
		orderitem.setCanRepay(json.optBoolean(ServerConstant.Order.CANREPAY));
		orderitem.setIsCompleted(json.optBoolean(ServerConstant.Order.IS_COMPLETED));
		orderitem.setIsCancelled(json.optBoolean(ServerConstant.Order.IS_CANCELLED));
		orderitem.setIsFullShipped(json.optBoolean(ServerConstant.Order.IS_FULLSHIPPED));
		orderitem.setIsShipping(json.optBoolean(ServerConstant.Order.IS_SHIPPING));
		orderitem.setDueAmount(json.optDouble(ServerConstant.Order.DUEAMOUNT));
		if (json.optString(ServerConstant.Order.ORDERSTATUS) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERSTATUS)) {
			orderitem.setOrderStatus(json
					.optString(ServerConstant.Order.ORDERSTATUS));
		}
		if (json.optString(ServerConstant.Order.LIGHT) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.LIGHT)) {
			orderitem.setLight(json.optString(ServerConstant.Order.LIGHT));
		}
		// 添加代码 by Tony
		if (json.optJSONArray(ServerConstant.Order.ORDERLINES) != null
				&& !json.isNull(ServerConstant.Order.ORDERLINES)) {
			List<Lines> linesList = new ArrayList<Lines>();
			JSONArray linesArray = json
					.optJSONArray(ServerConstant.Order.ORDERLINES);
			for (int i = 0; i < linesArray.length(); i++) {
				Lines line = new Lines();
				line = line.json2Object((JSONObject) linesArray.get(i));
				linesList.add(line);
			}
			orderitem.setLines(linesList);
		}
		//增加物流信息
		if (json.optJSONArray(ServerConstant.Order.PACKAGES) != null
				&& !json.isNull(ServerConstant.Order.PACKAGES)) {
			ArrayList<Package> packageList = new ArrayList<Package>();
			JSONArray pakageArray = json
					.optJSONArray(ServerConstant.Order.PACKAGES);
			for (int i = 0; i < pakageArray.length(); i++) {
				Package pack = new Package();
				pack = pack.json2Object((JSONObject) pakageArray.get(i));
				if(pack!=null){
					packageList.add(pack);
				}
			}
			orderitem.setPackages(packageList);
		}
		return orderitem;
	}

	/**
	 * 将值转换成json
	 * 
	 * @param jsonString
	 * @return [参数说明]
	 * @return OrderItemModel [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static OrderItemModel json2orderitem(String jsonString) {
		if (null == jsonString || "".equals(jsonString.trim())) {
			return null;
		}

		OrderItemModel orderitem = null;
		try {
			JSONObject json = new JSONObject(jsonString);
			orderitem = json2orderitem(json);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return orderitem;
	}

}